package com.threeonemeeting.task;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.threeonemeeting.Const;
import com.threeonemeeting.list.EnrollUserInfoList;
import com.threeonemeeting.model.EnrollUserInfo;

// 获取个人中心的消息列表
public class GetEnrollUserListTask extends ApplicationTask {

	public GetEnrollUserListTask(Context context, ICallBack callback) {
		super(context, callback);

		this.bCached = true;
		this.nTaskType = HttpConst.TASKTYPE_GETENROLLUSERS;
		this.strUrl = "http://new.31huiyi.com/json/GetEventJoinListHandler.ashx";
	}

	// 具体的task实现该方法，对响应报文进行解析
	protected Map<String, Object> parseResponse(String strResponse) {

		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put(HttpConst.KEY_TASK_TYPE, nTaskType);
		mapData.put(HttpUtil.KEY_ERROR_CODE, HttpUtil.ERROR_SUCCESS);

		JSONObject jsonResp = null;
		try {
			jsonResp = new JSONObject(strResponse);
		} catch (Exception e) {
			setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
			return mapData;
		}

		try {
			// 获取 "Setting"部分的内容
			String strSuccess = jsonResp.getString("success");
			if (strSuccess == null) {
				setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
				return mapData;
			}

			if (strSuccess.equalsIgnoreCase("true") == false) {
				setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
				return mapData;
			}

			JSONArray dataJson = jsonResp.getJSONArray("data");
			if (dataJson == null) {
				setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
				return mapData;
			}

			EnrollUserInfoList listUserInfo = new EnrollUserInfoList();
			for (int i = 0, n = dataJson.length(); i < n; i++) {

				JSONObject jData = dataJson.getJSONObject(i);

				EnrollUserInfo userInfo = new EnrollUserInfo();

				try {
					userInfo.nUserId = jData.getInt("Id");
					userInfo.strUserName = jData.getString("UserName");
					userInfo.strRealName = jData.getString(Const.Enroll.RealName);
					userInfo.nBuyCount = jData.getInt(Const.Enroll.BuyCount);
				} catch (JSONException ex) {
					continue;
				}

				try {
					userInfo.bGender = (jData.getInt(Const.Enroll.Sex) == 1 ? true
							: false);
				} catch (JSONException ex) {
					userInfo.bGender = true;
				}

				try {
					userInfo.nAge = jData.getInt(Const.Enroll.Age);
				} catch (JSONException ex) {
					userInfo.nAge = 0;
				}

				try {
					userInfo.strEmail = jData.getString(Const.Enroll.Email);
				} catch (JSONException ex) {
					userInfo.strEmail = "";
				}

				try {
					userInfo.strPhone = jData.getString(Const.Enroll.Phone);
				} catch (JSONException ex) {
					userInfo.strPhone = "";
				}

				try {
					userInfo.strFax = jData.getString(Const.Enroll.Fax);
				} catch (JSONException ex) {
					userInfo.strFax = "";
				}

				try {
					userInfo.strUserMobileNo = jData.getString(Const.Enroll.Mobile);
				} catch (JSONException ex) {
					userInfo.strUserMobileNo = "";
				}

				try {
					userInfo.strQQ = jData.getString(Const.Enroll.QQ);
				} catch (JSONException ex) {
					userInfo.strQQ = "";
				}

				try {
					userInfo.strMSN = jData.getString(Const.Enroll.MSN);
				} catch (JSONException ex) {
					userInfo.strMSN = "";
				}

				try {
					userInfo.strCompany = jData.getString(Const.Enroll.Company);
				} catch (JSONException ex) {
					userInfo.strCompany = "";
				}

				try {
					userInfo.strAddress = jData.getString(Const.Enroll.Address);
				} catch (JSONException ex) {
					userInfo.strAddress = "";
				}

				try {
					userInfo.strZipCode = jData.getString(Const.Enroll.Zipcode);
				} catch (JSONException ex) {
					userInfo.strZipCode = "";
				}

				try {
					userInfo.strMemo = jData.getString(Const.Enroll.Memo);
				} catch (JSONException ex) {
					userInfo.strMemo = "";
				}

				try {
					userInfo.nDepartment = jData.getInt("Department");
				} catch (JSONException ex) {
					userInfo.nDepartment = 0;
				}

				try {
					userInfo.nPosStatus = jData.getInt("PosStatus");
				} catch (JSONException ex) {
					userInfo.nPosStatus = 0;
				}

				try {
					userInfo.nIndustries = jData.getInt("Industries");
				} catch (JSONException ex) {
					userInfo.nIndustries = 0;
				}

				for (int j = 0; j < 10; j++) {

					try {

						Field field = userInfo.getClass().getField(
								"strField" + j);
						field.set(userInfo, jData.getString("Field" + j));

					} catch (SecurityException e) {
						e.printStackTrace();
					} catch (NoSuchFieldException e) {
						e.printStackTrace();
					} catch (JSONException ex) {
						userInfo.strMemo = "";
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}

				}

				listUserInfo.addEnrollUserInfo(userInfo);

			}

			mapData.put("list", listUserInfo);
			setErr(HttpUtil.ERROR_SUCCESS, mapData);
			return mapData;

		} catch (JSONException ex) {
			setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
			return mapData;
		}
	}
}
